//
//  File.swift
//  
//
//  Created by S JZ on 2024/1/31.
//

import Foundation

public extension String {
    public func getWidth(font: JZFont, size boundingRectWithSize: CGSize, lineSpacing: CGFloat = 0.0) -> CGFloat {
        return getSize(font: font, size: boundingRectWithSize, lineSpacing: lineSpacing).width
    }
    
    public func getHeight(font: JZFont, size boundingRectWithSize: CGSize, lineSpacing: CGFloat = 0.0) -> CGFloat {
        return getSize(font: font, size: boundingRectWithSize, lineSpacing: lineSpacing).height
    }
    
    public func getSize(font: JZFont, size boundingRectWithSize: CGSize, lineSpacing: CGFloat = 0.0) -> CGSize {
        var attributes: [NSAttributedString.Key : Any] = [:]
        attributes[NSAttributedString.Key.font] = font
        if lineSpacing > 0.0 {
            let style = NSMutableParagraphStyle()
            style.lineSpacing = lineSpacing
            attributes[NSAttributedString.Key.paragraphStyle] = style
        }
        let attributeStr = NSMutableAttributedString(string: self, attributes: attributes)
        let textRect = attributeStr.boundingRect(with: boundingRectWithSize, options: [.usesFontLeading, .usesLineFragmentOrigin], context: nil)
        return textRect.size
    }
}


